<?php 

class Html {
	
	var $tag;
	var $attrs;
	var $open_close;
	var $inner;
	
	public function __construct($tag, $attrs = array(), $open_close = TRUE){
		$this->tag = $tag;
		$this->attrs = $attrs;
		$this->open_close = $open_close;
	}
	
	public function open($attrs = array()){
		$this->attrs = array_merge($this->attrs, $attrs);
		$attrs = $this->renderAttributes();
		return "<{$this->tag} {$attrs}>";
	}
	
	public function close(){
		return "</{$this->tag}>";
	}
	
	public function unique($attrs = array()){
		$this->attrs = array_merge($this->attrs, $attrs);
		$attrs = $this->renderAttributes();
		return "<{$this->tag} {$attrs} />";
	}
	
	public function setInner($text = ""){
		$this->inner = $text;
	}
	
	public function render($name = NULL, $attrs = array()){
		if($this->open_close){
			return $this->open($attrs) . $this->inner . $this->close();
		}
		return $this->unique($attrs);
	}
	
	private function renderAttributes(){
		$attrs = array();
		if($this->open_close && array_key_exists("value", $this->attrs)){
			$this->setInner($this->attrs["value"]);
			unset($this->attrs["value"]);
		}
		foreach($this->attrs as $name => $value){
			$attrs[] = "{$name}=\"{$value}\"";
		}
		return implode(" ", $attrs);
	}
	
}

class Option {
	
	var $_value;
	var $_text;
	
	public function __construct($value, $text = NULL){
		$this->_value = $value;
		$this->_text = (NULL != $text) ? $text : $value;
	}
	
	public function render($selected){
		$selected = ($selected === TRUE) ? ' selected="selected"' : '';
		return "<option value=\"{$this->_value}\"{$selected}>{$this->_text}</option>";
	}
	
	public function getValue(){
		return $this->_value;
	}
	
}